<template>
  <div>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="$parent.open" width="780px" append-to-body>
      <div class="center_dialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="名称" prop="typeName">
            <el-input v-model="form.typeName" placeholder="请输入保养标准名称" />
          </el-form-item>
          <el-form-item label="周期" prop="cycle">
            <el-select v-model="form.cycle" placeholder="请选择周期" clearable :style="{ width: '100%' }">
              <el-option v-for="(item, index) in ['一日', '一周', '十五日', '一月', '三月', '六月', '十二月', '不定']" :key="index"
                :label="item" :value="index + ''"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="form.sort" placeholder="请输入排序" :style="{ width: '100%' }"
              controls-position="right" :min="1">
            </el-input-number>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
      </div>
      <div align="center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="() => $parent.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postCreatedchangeType, putUpdatechangeType } from '@/api/maintenance/insurance/insurance.js'
// import Upload from '@/components/upload/index.vue'
export default {
  props: ['title', 'rowObj'],
  components: {
    // Upload
  },
  data () {
    return {
      form: {
        "typeName": "", //类型名称
        "cycle": "", //周期（0一日 1一周 2十五日 3一月 4三月 5六月 6十二月 7不定）
        "sort": undefined, //排序
        "remark": "" //备注
      },
      rules: {
        typeName: { required: true, message: '必填', trigger: 'blur' },
        cycle: { required: true, message: '必填', trigger: 'blur' },
        sort: { required: true, message: '必填', trigger: 'blur' },
      }
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    getlist () {
      if (this.title === '修改保养类型') {//修改进入
        this.form = { ...this.rowObj }
      }
    },
    //提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '修改保养类型') {//修改
            let params = {
              ...this.form
            }
            putUpdatechangeType(params).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.$parent.open = false
                this.$parent.getList()//重新查询列表
              }
            })
          } else {//新增
            let params = {
              ...this.form
            }
            postCreatedchangeType(params).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.$parent.open = false
                this.$parent.handleUpdate()
              }
            })
          }
        }
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.center_dialog {
  // text-align: center;
  width: 50%;
  margin: 0px auto;
}
</style>